package com.runoob.gzip;

import cn.hutool.core.util.ZipUtil;

import java.io.*;
import java.util.zip.GZIPOutputStream;

public class Test1 {
    public static void compressFile(String filePath, String compressedFilePath) throws IOException {
        try (InputStream inputStream = new FileInputStream(filePath);
             OutputStream outputStream = new FileOutputStream(compressedFilePath);
             GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream)) {
            byte[] buffer = new byte[1024];
            int len;
            while ((len = inputStream.read(buffer)) != -1) {
                gzipOutputStream.write(buffer, 0, len);
            }
        }
    }

    public static void main(String[] args) {
        try {
            String filePath = "d:/MYSQL2.xml"; // 原始文件路径
            String compressedFilePath = "d:/MYSQL2.xml.gz"; // 压缩后的文件路径
            compressFile(filePath, compressedFilePath);
            System.out.println("文件成功压缩为: " + compressedFilePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
